package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.ApUserRealnameDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    public ResponseResult pageList(AuthDto dto) {
        dto.checkParam();
        //分页对象构造
        IPage<ApUserRealname> page = new Page(dto.getPage(),dto.getSize());
        //构造查询条件
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        //状态筛选
        wrapper.eq(dto.getStatus()!= null,ApUserRealname::getStatus,dto.getStatus());
        //查询结果
        page(page, wrapper);
        List<ApUserRealname> records = page.getRecords();
        ArrayList<ApUserRealnameDto> apUserRealnameDtos = new ArrayList<>();
        for (ApUserRealname record : records) {
            ApUserRealnameDto apUserRealnameDto = new ApUserRealnameDto();
            BeanUtils.copyProperties(record,apUserRealnameDto);
            ApUser apUser = apUserMapper.selectflagBName(record.getName());
            Short flag=1;
            if (apUser!=null){
               flag = apUser.getFlag();
            }
            apUserRealnameDto.setFlag(flag);
            apUserRealnameDtos.add(apUserRealnameDto);
        }
        //封装结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(apUserRealnameDtos);
        return result;
//        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
//        return ResponseResult.okResult(page.getRecords());
    }
}
